package com.jmpt.fangbohui.utils;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.util.TypedValue;

/**
 * 全局工具类
 * @author Victor  
 *
 */
public class AppUtils {
	public static float convertDpToPixel(float dp, Context context) {
		Resources resources = context.getResources();
		DisplayMetrics metrics = resources.getDisplayMetrics();
		float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
				metrics);
		return px;
	}

	public static float converPixelToDp(float px, Context context) {
		Resources resources = context.getResources();
		DisplayMetrics metrics = resources.getDisplayMetrics();
		float dp = px / (metrics.densityDpi / 160f);
		return dp;
	}

	public static float convertSpToPixel(float sp, Context context) {
		float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp,
				context.getResources().getDisplayMetrics());
		return px;
	}

	public static DisplayMetrics getDispalyMetrics(Context context) {
		DisplayMetrics metrics = new DisplayMetrics();
		((Activity) context).getWindowManager().getDefaultDisplay()
				.getMetrics(metrics);
		return metrics;
	}

	/**
	 * 获取版本号
	 * 
	 * @return 当前应用的版本名
	 */
	public static String getVersion(Context mContext) {
		try {
			PackageManager manager = mContext.getPackageManager();
			PackageInfo info = manager.getPackageInfo(
					mContext.getPackageName(), 0);
			String version = info.versionName;
			return version;
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	public static int getVersionCode(Context context) {
		PackageInfo pinfo = null;
		try {
			pinfo = ((Activity) context).getPackageManager().getPackageInfo(
					((Activity) context).getPackageName(),
					PackageManager.GET_CONFIGURATIONS);
			int versionCode = pinfo.versionCode;
			return versionCode;
		} catch (PackageManager.NameNotFoundException e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	public static long phpTimeStamp2Java(long timestamp) {
		return timestamp * 1000;
	}

	public static long JavaTimeStamp2php(long timestamp) {
		return Math.abs(timestamp / 1000);
	}

}
